const fp = require('lodash/fp');
const {Maybe, Container} = require('./support');

// 练习1
let maybe = Maybe.of([5, 6, 1])
let ex1 = (x) => {
    return maybe.map(fp.map(fp.add(x)))
};
console.log(ex1(1)._value) // => [6, 7, 2]
console.log(ex1(2)._value) // => [7, 8, 3]

// 练习2
let xs = Container.of(['do', 'ray', 'me', 'fa', 'so', 'la', 'ti', 'do']);
let ex2 = () => {
    return xs.map(fp.first)._value
};
console.log(ex2()) // => do

// 练习3
let safeProp = fp.curry(function(x, o) {
    return Maybe.of(o[x])
})
let user = {id: 2, name: 'Albert'}
let ex3 = () => {
    let safePropName = safeProp('name');
    return safePropName(user).map(fp.first)._value
}
console.log(ex3()) // => A

// 练习4
let ex4 = function(n) {
    if (n) {
        return parseInt(n)
    }
}

let rewriteEx4 = function(n) {
    return Maybe.of(n).map(parseInt)._value
}

console.log(rewriteEx4('7.7')) // => 7
console.log(rewriteEx4(0)) // => 0
console.log(rewriteEx4(null)) // => null
console.log(rewriteEx4(undefined)) // => undefined
console.log(rewriteEx4(NaN)) // => NaN